
package laya.semi.service;

import java.util.*;
import java.util.regex.*;
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.*;

import org.apache.log4j.*;

import laya.*;
import laya.util.*;
import laya.xml.sax.*;

public class GenericServiceHandlerConfig extends DefaultHandler
{
	private static Logger log = Logger.getLogger(GenericServiceHandlerConfig.class);

	public static final String TAG_ROOT = LayaGlobals.LAYA_NAMESPACE + ":" + "GenericServices";
	public static final String TAG_SERVICENODE = LayaGlobals.LAYA_NAMESPACE + ":" + "GenericService";
	public static final String TAG_SERVICECLASS = LayaGlobals.LAYA_NAMESPACE + ":" + "ServiceClass";
	public static final String TAG_SERVICEDESCRIPTION = LayaGlobals.LAYA_NAMESPACE + ":" + "ServiceClassDescription";
	public static final String ATTRIBUTE_SERVICEID = "id";
	
	public static final int STATE_READY = 0;
	public static final int STATE_START = 1;
	public static final int STATE_START_SERVICENODE = 2;
	public static final int STATE_START_SERVICECLASS = 3;
	public static final int STATE_GET_SERVICECLASS = 4;
	public static final int STATE_START_SERVICEDESCRIPTION = 5;
	public static final int STATE_GET_SERVICEDESCRIPTION = 6;
	public static final int STATE_END_SERVICEDESCRIPTION = 7;
	public static final int STATE_END_SERVICENODE = 8;
	public static final int STATE_END = 9;
	
	private int m_nStep = STATE_READY;
	private Hashtable m_oClassBeanTab = null;
	private Hashtable m_oServiceBeanTab = null;
	private StringBuffer m_oContentBuffer = null;
	
	private String m_oServiceId = null;

	public IServiceBean getServiceBean(String oSvcId)
	{
		return (IServiceBean)m_oServiceBeanTab.get(oSvcId);
	}
	
	public void startDocument()
		throws SAXException
	{
		// log.debug("startDocument()");
		m_oClassBeanTab = new Hashtable();
		m_oServiceBeanTab = new Hashtable();
		m_nStep = STATE_START;
	}
	
	public void startElement(String uri, String localName, String qName, Attributes attributes)
		throws SAXException
	{
		// log.debug("startElement(" + uri + ", " + localName + ", " + qName + ")");
		switch(m_nStep)
		{
			case STATE_START:
			{
				if (!TAG_ROOT.equals(qName))
				{
					throw new SAXParseException(1004, m_nStep, qName, TAG_ROOT);
				}
				
				m_nStep = STATE_START_SERVICENODE;
				break;
			}
			case STATE_START_SERVICENODE:
			{
				if (!TAG_SERVICENODE.equals(qName))
				{
					throw new SAXParseException(1004, m_nStep, qName, TAG_SERVICENODE);
				}
				
				m_oServiceId = attributes.getValue(ATTRIBUTE_SERVICEID);
				if (null == m_oServiceId)
				{
					throw new SAXParseException(1005, TAG_SERVICENODE);
				}
				else if (null != m_oServiceBeanTab.get(m_oServiceId))
				{
					throw new SAXParseException(1215, m_oServiceId);
				}
				
				m_nStep = STATE_START_SERVICECLASS;
				break;
			}
			case STATE_START_SERVICECLASS:
			{
				if (!TAG_SERVICECLASS.equals(qName))
				{
					throw new SAXParseException(1004, m_nStep, qName, TAG_SERVICECLASS);
				}
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_SERVICECLASS;
				break;
			}
			case STATE_START_SERVICEDESCRIPTION:
			{
				if (!TAG_SERVICEDESCRIPTION.equals(qName))
				{
					throw new SAXParseException(1004, m_nStep, qName, TAG_SERVICEDESCRIPTION);
				}
				
				m_oContentBuffer = new StringBuffer();
				m_nStep = STATE_GET_SERVICEDESCRIPTION;
				break;
			}
			default:
			{
				throw new SAXParseException(1002, m_nStep, qName);
			}
		}
	}
	
	public void characters(char[] ch, int start, int length)
		throws SAXException
	{
		switch(m_nStep)
		{
			case STATE_GET_SERVICECLASS:
			//case STATE_GET_SERVICEDESCRIPTION:
			{
				m_oContentBuffer.append(ch, start, length);
				break;
			}
		}
	}
	
	public void endElement(String uri, String localName, String qName)
		throws SAXException
	{
		switch(m_nStep)
		{
			case STATE_GET_SERVICECLASS:
			{
				String oServiceClassName = m_oContentBuffer.toString().trim();
				IServiceBean oBean = (IServiceBean)m_oClassBeanTab.get(oServiceClassName);
				
				if (null == oBean)
				{
					try
					{
						oBean = (IServiceBean)Class.forName(oServiceClassName).newInstance();
					}
					catch(Exception e)
					{
						log.warn(MessageResource.getDefaultInstance().getMessage("laya.exception.1214", new String[] {oServiceClassName}), e);
						throw new SAXParseException(1214, oServiceClassName);
					}
					
					m_oClassBeanTab.put(oServiceClassName, oBean);
				}
				
				m_oServiceBeanTab.put(m_oServiceId, oBean);
				
				m_nStep = STATE_START_SERVICEDESCRIPTION;
				break;
			}
			case STATE_GET_SERVICEDESCRIPTION:
			{
				m_nStep = STATE_END_SERVICEDESCRIPTION;
				break;
			}
			case STATE_END_SERVICEDESCRIPTION:
			case STATE_START_SERVICEDESCRIPTION:
			{
				m_nStep = STATE_START_SERVICENODE;
				break;
			}
			case STATE_START_SERVICENODE:
			{
				m_nStep = STATE_END;
				break;
			}
			default:
			{
				throw new SAXParseException(1003, m_nStep, qName);
			}
		}
	}
	
	public void endDocument()
		throws SAXException
	{
		// log.debug("endDocument()");
		
		switch(m_nStep)
		{
			case STATE_START:
			{
				log.warn(MessageResource.getDefaultInstance().getMessage("laya.info.ServiceBeanConfigEmpty"));
				break;
			}
			case STATE_END:
			{
				log.info(MessageResource.getDefaultInstance().getMessage("laya.info.ServiceBeanConfigLoadOk"));
				break;
			}
			default:
			{
				throw new SAXParseException(1001, m_nStep);
			}
		}
	}
}
